package pt.ipleiria.plp;

import pt.ipleiria.plp.threads.CallbackRetrievePos;
import pt.ipleiria.plp.threads.CallbackValidKey;
import de.enough.polish.io.RmsStorage;
import java.io.IOException;
//#ifdef polish.api.locationapi
import javax.microedition.location.LocationException;
//#endif
import pt.ipleiria.plp.threads.ObtainPosition;
import pt.ipleiria.plp.threads.ValidateKey;

/**
 * Main class of the controller part of this application
 * @author Filipe Felisberto e Ricardo Silva
 */
public class Application {

    private ApplicationData appData;
    private LixeirasManager manager;
    private RmsStorage storage;
    private ValidateKey val;
    //#ifdef polish.api.locationapi
    private GPS gps;
    private ObtainPosition obt;
    //#endif

    /**
     * Empty constructor that will get all necessary data from storage or
     * create a new one
     */
    public Application() {
        storage = new RmsStorage();
        try {

            this.appData = (ApplicationData) this.storage.read("plp_storage");

        } catch (IOException ex) {
            this.appData = new ApplicationData();
        }

        if (appData.getApp() != null) {
            manager = new LixeirasManager(getAppData().getApp());
        } else {
            manager = new LixeirasManager();
        }
        //#ifdef polish.api.locationapi
        gps = null;
        //#endif

    }

    /**
     * Persists the both the application data and the Lixeiras
     * @throws IOException
     */
    public void save() throws IOException {

        saveAppData();
        manager.save();

    }

    /**
     * Persists the application data
     * @throws IOException
     */
    private void saveAppData() throws IOException {

        this.storage.save(getAppData(), "plp_storage");

    }

    /**
     * Returns the aplication key currently being used
     *
     * @return the app key
     */
    public String getAppKey() {
        return getAppData().getApp();
    }

    /**
     * Sets the new app key
     *
     * @param app the new key
     * @return true if set successfull
     */
    public void setAppKey(String app) throws IOException {


        getAppData().setApp(app);

        saveAppData();

        manager = new LixeirasManager(app);



    }

    /**
     * Validates if the key being set is registered on the 3rd block data base
     *
     * @param app the nkey to test
     * @return true if the test was successefull
     */
    public void validateAppKey(String app, CallbackValidKey obj) {

        val = new ValidateKey(app, obj);
        val.start();

    }

    /**
     * @return the manager
     */
    public LixeirasManager getManager() {
        return manager;
    }

    /**
     * Verifies if the application has a gps configured
     * @return true if the gps is configured
     */
    public boolean hasGps() {

        //#ifdef polish.api.locationapi
        return (gps == null) ? false : true;
        //#else
        //#return false;
        //#endif

        

    }


    /**
     * Tries to get a gps provider
     * @return true if successfull
     */
    public boolean acquireGPS() {
        //#ifdef polish.api.locationapi
        try {
            gps = new GPS();
            return true;
        } catch (LocationException ex) {
            gps = null;
            return false;
        }
        //#else
        //#return false;
        //#endif

    }

    /**
     * retrieves the current position
     * @return the current position
     */
    public void getCords(CallbackRetrievePos obj) {
        //#if polish.api.locationapi
        obt = new ObtainPosition(gps, obj);
        obt.start();
        //#endif
    }


    /**
     * @return the appData
     */
    public ApplicationData getAppData() {
        return appData;
    }
}

